package com.bw.services.visit

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.adapters.ViewBindingAdapter.setPadding
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.common.utils.ImmersiveNightModeUtil
import com.bw.lib_base.base.BaseActivity
import com.bw.services.R
import com.bw.services.databinding.ActivityVisitBinding
import com.bw.services.visit.adapter.VisitAdapter
import com.bw.services.visit.entity.VisitData
import com.bw.services.visit.entity.VisitEntity
import com.bw.services.visit.model.VisitViewModel
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@AndroidEntryPoint
@Route (path = "/services/visit/VisitActivity")
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitViewModel>() {

    val staticList = listOf("全部状态", "待审核", "已通过", "已拒绝", "已完成","驳回")
    private var visitAdapter  = VisitAdapter()
    private var visitData: List<VisitData> = emptyList()

    override fun generatePremission(pageName: String): Array<String> {
        TODO("Not yet implemented")
    }

    override fun setupUI() {
        binding.rvVisit.apply {
            visitAdapter = VisitAdapter()
            layoutManager=LinearLayoutManager(this@VisitActivity)
            binding.rvVisit.adapter = visitAdapter
        }
        // 初始化Tab
        staticList.forEach { status ->
            binding.visitRvstatus.addTab(binding.visitRvstatus.newTab().setText(status))
        }


        // Tab切换监听
        binding.visitRvstatus.addOnTabSelectedListener(object : com.google.android.material.tabs.TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: com.google.android.material.tabs.TabLayout.Tab?) {
                filterListByStatus(tab?.position ?: 0)
            }
            override fun onTabUnselected(tab: com.google.android.material.tabs.TabLayout.Tab?) {}
            override fun onTabReselected(tab: com.google.android.material.tabs.TabLayout.Tab?) {}
        })
    }

    override fun fetchData() {
       vm.getVisitData()
    }

    override fun observeData() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.visitData.collect{
                    if (it != null) {
                        visitData = it
                        // 默认显示全部
                        filterListByStatus(binding.visitRvstatus.selectedTabPosition)
                    } else {
                        Log.i("===", "123:${it}")
                    }
                }
            }
        }
    }

    // 根据Tab筛选数据
    private fun filterListByStatus(tabIndex: Int) {
        val filtered = when (tabIndex) {
            0 -> visitData // 全部
            else -> {
                val status = staticList[tabIndex]
                visitData.filter { it.visitStatus == status }
            }
        }
        visitAdapter.setData(filtered)
    }

    override fun handleEvent() {

    }

    override val mLayoutResId: Int
        get() = TODO("Not yet implemented")
    // 扩展函数：dp转px
    fun Int.dpToPx(): Int = (this * resources.displayMetrics.density).toInt()
}